require('../../assets/css/index.less');
require('./play.less');

require('../../assets/fitness-font/iconfont.css');
// 引入axios
const axios = require('../../utils/axios.js');

// 引入window
const { $, g } = require('../../utils/dom');



/**
 * window加载事件
 */
$(function () {

    // 获取本地视频路径
    let fragments = JSON.parse(localStorage.getItem('fragments'));
    console.log('fragments :>> ', fragments);

    // 视频索引
    let index = 0;

    // 运动总时间
    let alltime = 0;

    // 开始运动时间
    let starttime = Date.now()

    function thePlay() {

        g('#video').src = 'http://fitness.h5.itsource.cn:3701' + fragments[index].videoUrl;

        // 播放
        setTimeout(() => {
            video.play();
        }, 500)

        // 渲染
        g('#allcount').textContent = fragments.length;
        g('#courcount').textContent = index + 1;
        g('.name').textContent = fragments[index].title;
    }
    thePlay();

    // 自动播放
    video.addEventListener('ended', () => {
        ++index;
        if (index >= fragments.length) return;
        thePlay();
    })

    // 点击下一首
    g('#nextbtn').addEventListener('click', () => {
        ++index;
        if (index >= fragments.length) {
            index = 0;
        };
        g('#playbtn').style.display = 'none';
        g('#pausebtn').style.display = 'block';
        thePlay();
    })

    // 点击上一首
    g('#prevbtn').addEventListener('click', () => {
        --index;
        console.log('index :>> ', index);
        if (index < 0) {
            index = fragments.length - 1;
        };
        g('#playbtn').style.display = 'none';
        g('#pausebtn').style.display = 'block';
        thePlay();
    })

    // 切换播放和暂停
    g('#playbtn').addEventListener('click', () => {
        video.play();
        g('#playbtn').style.display = 'none';
        g('#pausebtn').style.display = 'block';
    })

    g('#pausebtn').addEventListener('click', () => {
        video.pause();
        g('#playbtn').style.display = 'block';
        g('#pausebtn').style.display = 'none';
    })

    // 进度条
    g('#video').addEventListener('timeupdate', function () {
        g('.progress').style.width = this.currentTime / this.duration * 100 + '%'
    })

    // 暂停播放
    g('#playbtn').addEventListener('click', () => {
        video.pause();
        g('.mask').style.display = 'block';

        // 累计运动时间
        alltime += Date.now() - starttime

        starttime = Date.now() //继续播放，开始时间就是当前时间
    })

    // 继续播放
    g('#continuebtn').addEventListener('click', () => {
        video.play();
        g('.mask').style.display = 'none';
    })

    // 结束播放
    g('#stopbtn').addEventListener('click', () => {
        // 获取训练总时间
        console.log('alltime :>> ', alltime);

        // 参数对象
        const params = {
            type: 1,
            duration: Math.ceil(alltime / 1000), //运动总时间
            calorie: Math.ceil(alltime / 1000) * 1.5,
            meter: Math.ceil(alltime / 1000) * 3
        }
        axios.post('/api/exercise/save', params).then(res => {
            if (res.data.errno === 0) {
                location.href = './sportsCoures.html';
            }
        })
    })
})
